#include<stdio.h>
#include<malloc.h>
//#include<conio.h>
#define null 0
void create();
void display();
void insert();
void delet();
void erase();
void reverse();

struct node
{
	int info;
	int pos;
	struct node *next;
}*start,*ptr,*temp,*prev,*start2,*last;

int main()
{
	int op;
	//clrscr();
	do
	{
		printf("\n1.link1\n2.link2\n3.merge\n4.exit");
		printf("\nEnter your option");
		scanf("%d",&op);
		switch(op)
		{
		case 1:  link1();
			break;

		case 2: link2();
			break;

		case 3: extend();
			break;

		case 4: break;
			default:
			printf("\nEnter correct option");
		}
	}while(op!=4);
	return 0;
	getch();

}
void create()
{	int pos=0;
	int data;
	printf("\nEnter the data & Press 0 to terminate=");
	scanf("%d",&data);
	do
	{
		ptr=malloc(sizeof(struct node));
		ptr->info=data;
		ptr->pos=pos++;
		ptr->next=NULL;
		if(start==NULL)
			start=last=ptr;
		else
		{
			last->next=ptr;
			last=ptr;
			/*temp=start;
			while(temp->next!=NULL)
			{
				temp=temp->next;
			}*/
			//temp->next=ptr;
			//last=ptr;
		}
		printf("\nEnter the data & Press 0 to terminate=");
		scanf("%d",&data);
	}while(data!=0);
}


void display()
{
temp=start;
if(start==NULL)
printf("\nList is empty");
else
{
printf("\nElements of list:=\n");
while(temp->next!=NULL)
{
printf("%d\t",temp->info);
temp=temp->next;
}
printf("%d\t",temp->info);
}
}


void insert()
{
	int data,pos;
	printf("\nEnter data & position to insert the element=");
	scanf("%d %d",&data,&pos);
	temp=start;
	ptr=malloc(sizeof(struct node));
	ptr->info=data;
	ptr->next=NULL;
	prev=NULL;
	while(temp!=NULL)
	{	if(temp->pos==pos)
		{	if(pos==0)
			{	ptr->next=start;
				 start=ptr;
			}
			else
			{	prev->next=ptr;
				ptr->next=temp;
			}
		}
		prev=temp;
		temp=temp->next;
	}
}

/*while(temp->next!=NULL)
{
	if(temp->info==pos)
	{
		ptr->next=temp->next;
		temp->next=ptr;
		break;
	}
	else
		temp=temp->next;
}
if(temp->next==NULL)
{
	temp->next=ptr;
	ptr->next=NULL;
}*/



void delet()
{
int pos,flag=0;
if(start==NULL)
printf("List is empty");
else
{
printf("Enter element to be deleted=");
scanf("%d",&pos);
temp=start;
if(start->info==pos)
{
flag=1;
start=start->next;
free(temp);
}
else
{
while(temp->next!=NULL)
{
prev=temp;
temp=temp->next;
if(temp->info==pos)
{
flag=1;
prev->next=temp->next;
free(temp);
break;
}
}
}
if(flag==0)
printf("\nElement is not present in list");
}
}


void erase()
{
if(start==NULL)
printf("\nList is empty!!");
else
{
while(start->next!=NULL)
{
temp=start;
start=start->next;
free(temp);
}
temp=start;
start=start->next;
free(temp);
printf("list is erased");
}
}


void reverse(/*struct node *head*/) //reversing linked list
{
//struct node *head;
  //start=head;
  temp=NULL;
  while(prev!=NULL)
 {
  start=temp;
  temp=prev;
  prev=prev->next;
  temp->next=start;
 }
  ptr=temp;
display();

}


int link1()
{
	int op;
	start=NULL;
	do
	{
		printf("\n1.create\n2.display\n3.insert\n4.delete\n5.erase\n6.reverse\n7.exit");
		printf("\nEnter your option: ");
		scanf("%d",&op);
		switch(op)
		{
		case 1: create();
			break;
		case 2: display();
			break;
		case 3: insert();
			break;
		case 4: delet();
			break;
		case 5: erase();
			break;
		case 6: reverse();
			break;
		case 7:
			break;
		default:
			printf("\nEnter correct option!!!");
		}
	}while(op!=7);
	return 0;
	getch();
}


int link2()
{
	int op;
	start=NULL;
do
{
printf("\n1.create\n2.display\n3.insert\n4.delete\n5.erase\n6.reverse\n7. exit");
printf("\nEnter your option");
scanf("%d",&op);
switch(op)
{
case 1:create();
break;
case 2:display();
break;
case 3:insert();
break;
case 4:delet();
break;
case 5: erase();
break;
case 6:reverse();
break;
case 7:
break;
default:
printf("\nEnter correct option!!!");
}
}while(op!=7);
return 0;
getch();
}

int extend()
{
if(start==NULL)
{
start=start2;
}
else
{
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=start2;
}
temp=start;
if(start==NULL)
printf("\nList is empty!!!");
else
{
printf("\nElements of list:\n");
while(temp!=NULL)
{
printf("%d\t",temp->info);
temp=temp->next;
}
}
//getch();
}
